package com.atguigu.cloud.controller;/**
 * @Auter 李孝在
 * @Date 2024/11/18
 */

import com.atguigu.cloud.entities.Collect;
import com.atguigu.cloud.entities.LoginDTO;
import com.atguigu.cloud.sevice.AdminService;
import com.atguigu.cloud.sevice.MerchantService;
import com.atguigu.cloud.sevice.UserService;
import com.atguigu.cloud.tool.TokenAbout.TokenBuilder;
import com.atguigu.cloud.tool.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.controller
 * @className: LoginController
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/18 03:23
 * @version: 1.0
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Resource
    private UserService userService;
    @Resource
    private MerchantService merchantService;
    @Resource
    private AdminService adminService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


//    //小程序用户登录接口
    @PostMapping(value = "/wechat")
    @Operation(summary = "微信登录接口", description = "需要提供请求体提供认证对象,即使用表单提交username和password(形参不可以变)")
    public ResultData<Map<String,Object>> loginFromWeChat(@RequestBody LoginDTO user){

        String username = user.getName();//用户名

        //搜索id
        Integer userid=userService.getIdByName(username);
        //生成token
        String token = TokenBuilder.getTokenByUUID().toString();
        //将token存入redis中
        stringRedisTemplate.opsForValue().set(token,userid.toString(),30, TimeUnit.MINUTES);

        Map<String,Object> useridAndToken =new HashMap<>();
        useridAndToken.put("userid",userid);
        useridAndToken.put("tokne",token);
        TokenBuilder.getTokenByUUID();
        return ResultData.success(useridAndToken);

    }

    //web商户登录接口
    @PostMapping(value = "/WebMerchant")
    @Operation(summary = "Web商户登录接口", description = "需要提供请求体提供认证对象,即使用表单提交username和password(形参不可以变)")
    public ResultData<Map<String,Object>> loginFromWebMerchant(@RequestBody LoginDTO user){

        String username = user.getName();//用户名


        Integer userid=merchantService.getIdByName(username);
        String token = TokenBuilder.getTokenByUUID().toString();

        //将token存入redis中
        stringRedisTemplate.opsForValue().set(token,userid.toString(),30, TimeUnit.MINUTES);

        Map<String,Object> useridAndToken =new HashMap<>();
        useridAndToken.put("userid",userid);
        useridAndToken.put("tokne",token);
        TokenBuilder.getTokenByUUID();
        return ResultData.success(useridAndToken);

    }

    //Admin管理登录接口
    @PostMapping(value = "/WebAdmin")
    @Operation(summary = "WebAdmin登录接口", description = "需要提供请求体提供认证对象,即使用表单提交username和password(形参不可以变)")
    public ResultData<Map<String,Object>> loginFromWebAdmin(@RequestBody LoginDTO user){

        String username = user.getName();//用户名

        Integer userid=adminService.getIdByName(username);
        String token = TokenBuilder.getTokenByUUID().toString();

        //将token存入redis中
        stringRedisTemplate.opsForValue().set(token,userid.toString(),30, TimeUnit.MINUTES);

        Map<String,Object> useridAndToken =new HashMap<>();
        useridAndToken.put("userid",userid);
        useridAndToken.put("tokne",token);
        TokenBuilder.getTokenByUUID();
        return ResultData.success(useridAndToken);
    }


}
